﻿use SSODemo;

--region Drop Existing Procedures

IF OBJECT_ID(N'[usp_InsertAccountApp]') IS NOT NULL
	DROP PROCEDURE [usp_InsertAccountApp]

IF OBJECT_ID(N'[usp_UpdateAccountApp]') IS NOT NULL
	DROP PROCEDURE [usp_UpdateAccountApp]

IF OBJECT_ID(N'[usp_DeleteAccountApp]') IS NOT NULL
	DROP PROCEDURE [usp_DeleteAccountApp]

IF OBJECT_ID(N'[usp_SelectAccountApp]') IS NOT NULL
	DROP PROCEDURE [usp_SelectAccountApp]

IF OBJECT_ID(N'[usp_SelectAccountAppsAll]') IS NOT NULL
	DROP PROCEDURE [usp_SelectAccountAppsAll]

IF OBJECT_ID(N'[usp_DeleteAccountAppsByAccountID]') IS NOT NULL
	DROP PROCEDURE [usp_DeleteAccountAppsByAccountID]

IF OBJECT_ID(N'[usp_DeleteAccountAppsByAppID]') IS NOT NULL
	DROP PROCEDURE [usp_DeleteAccountAppsByAppID]
	
IF OBJECT_ID(N'[usp_SelectAccountAppsDynamic]') IS NOT NULL
	DROP PROCEDURE [usp_SelectAccountAppsDynamic]

IF OBJECT_ID(N'[usp_SelectAccountAppsByAccountID]') IS NOT NULL
	DROP PROCEDURE [usp_SelectAccountAppsByAccountID]

IF OBJECT_ID(N'[usp_SelectAccountAppsByAppID]') IS NOT NULL
	DROP PROCEDURE [usp_SelectAccountAppsByAppID]

--endregion

GO

--region [usp_InsertAccountApp]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_InsertAccountApp]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_InsertAccountApp]
	@AccountID uniqueidentifier,
	@AppID uniqueidentifier,
	@DateCreated datetime,
	@ExpireTime datetime,
	@ID uniqueidentifier
AS

SET NOCOUNT ON

IF EXISTS (SELECT null FROM [AccountApp]
	WHERE [AccountID] = @AccountID AND [AppID] = @AppID)
	return;
ELSE
BEGIN
INSERT INTO [AccountApp] (
	[ID],
	[AccountID],
	[AppID],
	[DateCreated],
	[ExpireTime]
) VALUES (
	@ID,
	@AccountID,
	@AppID,
	@DateCreated,
	@ExpireTime
)
END
--endregion

GO

--region [usp_UpdateAccountApp]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_UpdateAccountApp]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_UpdateAccountApp]
	@ID uniqueidentifier,
	@AccountID uniqueidentifier,
	@AppID uniqueidentifier,
	@DateCreated datetime,
	@ExpireTime datetime
AS

SET NOCOUNT ON

UPDATE [AccountApp] SET
	[AccountID] = @AccountID,
	[AppID] = @AppID,
	[DateCreated] = @DateCreated,
	[ExpireTime] = @ExpireTime
WHERE
	[ID] = @ID

--endregion

GO

--region [usp_DeleteAccountApp]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_DeleteAccountApp]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_DeleteAccountApp]
	@ID uniqueidentifier
AS

SET NOCOUNT ON

DELETE FROM [AccountApp]
WHERE
	[ID] = @ID

--endregion

GO

--region [usp_DeleteAccountAppsByAccountID]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_DeleteAccountAppsByAccountID]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_DeleteAccountAppsByAccountID]
	@AccountID uniqueidentifier
AS

SET NOCOUNT ON

DELETE FROM [AccountApp]
WHERE
	[AccountID] = @AccountID

GO

--endregion

GO

--region [usp_DeleteAccountAppsByAppID]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_DeleteAccountAppsByAppID]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_DeleteAccountAppsByAppID]
	@AppID uniqueidentifier
AS

SET NOCOUNT ON

DELETE FROM [AccountApp]
WHERE
	[AppID] = @AppID

GO

--endregion

GO

--region [usp_SelectAccountApp]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_SelectAccountApp]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_SelectAccountApp]
	@ID uniqueidentifier
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[AccountID],
	[AppID],
	[DateCreated],
	[ExpireTime]
FROM
	[AccountApp]
WHERE
	[ID] = @ID

--endregion

GO

--region [usp_SelectAccountAppsAll]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_SelectAccountAppsAll]
-- Date Generated: 2009-04-28
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_SelectAccountAppsAll]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[AccountID],
	[AppID],
	[DateCreated],
	[ExpireTime]
FROM
	[AccountApp]

--endregion

GO

--region [usp_SelectAccountAppsByAccountID]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_SelectAccountAppsByAccountID]
-- Date Generated: 2009-04-29
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_SelectAccountAppsByAccountID]
	@AccountID uniqueidentifier
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[AccountID],
	[AppID],
	[DateCreated],
	[ExpireTime]
FROM
	[AccountApp]
WHERE
	[AccountID] = @AccountID

--endregion

GO

--region [usp_SelectAccountAppsByAppID]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_SelectAccountAppsByAppID]
-- Date Generated: 2009-04-29
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_SelectAccountAppsByAppID]
	@AppID uniqueidentifier
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[AccountID],
	[AppID],
	[DateCreated],
	[ExpireTime]
FROM
	[AccountApp]
WHERE
	[AppID] = @AppID

--endregion

GO

--region [usp_SelectAccountAppsDynamic]

------------------------------------------------------------------------------------------------------------------------
-- Generated By:   Zyg
-- Procedure Name: [usp_SelectAccountAppsDynamic]
-- Date Generated: 2009-04-29
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [usp_SelectAccountAppsDynamic]
	@WhereCondition nvarchar(500),
	@OrderByExpression nvarchar(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT
	[ID],
	[AccountID],
	[AppID],
	[DateCreated],
	[ExpireTime]
FROM
	[AccountApp]
WHERE
	' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + '
ORDER BY
	' + @OrderByExpression
END

EXEC sp_executesql @SQL

--endregion

GO
